package appBord

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject

class AppBordTypeController {

    def index() {
       // render view: "/index.gsp"
       render AppBordType.list() as JSON
    }

    def initData(){
        AppBordType appBordType = new AppBordType(typeName: "正常",typeNo: "RUN")
        appBordType.save(flush: true)
    }
    def show(){
        def appBordType
        if(params?.id){
            appBordType = AppBordType.get(params?.id)
        }else{
            appBordType = AppBordType.newInstance()
        }
        render(view: "/appBord/appBordType/update.gsp",model: [appBordType:appBordType])
    }

    def update(){

        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")
        resultJson.put("closeCurrent",true)

        try {
            def typeNo = params.typeNo
            def appBordType = AppBordType.findByTypeNo(typeNo)
            if(!appBordType){
                appBordType = AppBordType.newInstance()
            }

            def typeName = params.typeName
            def typeColor = params.typeColor

            appBordType.typeNo = typeNo
            appBordType.typeName = typeName
            appBordType.typeColor = typeColor
            appBordType.save(flush: true)
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","操作失败")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }

    def delete(){
        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")

        try {
            def  appBordType = AppBordType.get(params?.id)
            appBordType.delete(flush: true)
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","操作失败")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }
}
